package com.ggs.rg.ybjb.usermodule.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.pojomodule.pojo.Mater;
import com.ggs.rg.ybjb.pojomodule.pojo.TComment;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.usermodule.service.MaterService;
import com.ggs.rg.ybjb.usermodule.service.TCommentService;
import com.ggs.rg.ybjb.usermodule.util.ImageUtil;
import com.ggs.rg.ybjb.usermodule.util.ListPagesUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxw
 * @since 2021-11-04
 */
/*
查看评论  管理员和用户查看所有评论
添加品论   用户发布评论   管理员回复评论
删除评论  管理员删除评论
 */

@RestController
@RequestMapping("/comment")
public class TCommentController {

    @Autowired
    TCommentService tCommentService;

    @Autowired
    MaterService materService;

    /*
    发布评论，
    用户只能在一个商品发布一个评论，管理员也只能回复一次。
     */
    @PostMapping("/addUserComment")
    public JSONObject addUserComment(@RequestParam("text") String text,
                                     @RequestParam("pid") Integer pid,
                                     @RequestParam("phone") String phone,
                                     @RequestParam("oid") String oid,
                                     @RequestParam(value = "p1",required = false) MultipartFile p1){
        //查询所有评论，筛选该的商品所有评论，评判断用户是否已经评论
        QueryWrapper<TComment> queryWrapper = new QueryWrapper<>();
        QueryWrapper<TComment> select = queryWrapper.eq("oid", oid);
        List<TComment> list = tCommentService.list(select);
        List<Integer> pidList = new ArrayList<>();
        List<Integer> List1 = new ArrayList<>();
        QueryWrapper<Mater> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Mater> select1 = queryWrapper1.select("pid");
        List<Mater> list1 = materService.list(select1);
        //获取评论表内相关订单的所有商品参数id
        for (TComment tc : list){
            Integer pid1 = tc.getPid();
            pidList.add(pid1);
        }
        //获取参数表商品id 判断用户评论的商品参数id是否存在
        for (Mater m : list1){
            int pid1 = m.getPid();
            List1.add(pid1);
        }
        if (StringTools.isNullOrEmpty(p1)){
            if (!pidList.contains(pid)){
                if (List1.contains(pid)){
                    TComment tComment = new TComment(text,phone,pid,oid);
                    tCommentService.save(tComment);
                    return CommonUtil.successJson();
                }else {
                    return CommonUtil.sqlErrorJson("该规格商品已被下架");
                }
            }else {
                return CommonUtil.sqlErrorJson("false");
            }
        }else {
            //工具类获取图片
            String image = ImageUtil.getNewFileName(p1);
            if (!pidList.contains(pid)){
                if (List1.contains(pid)){
                    TComment tComment = new TComment(text,image,phone,pid,oid);
                    tCommentService.save(tComment);
                    //保存图片到服务器
                    try {
                        ImageUtil.saveFile(image,p1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return CommonUtil.successJson();
                }else {
                    return CommonUtil.sqlErrorJson("该规格商品已被下架");
                }
            }else {
                return CommonUtil.sqlErrorJson("false");
            }
        }
    }
}

